package com.example.baiduapi.Config;

import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Sa-Token 权限拦截器配置
 */
//@Configuration
public class SaTokenConfig implements WebMvcConfigurer {

//    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验
        registry.addInterceptor(new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                // 校验登录状态
                StpUtil.checkLogin();
                return true;
            }
        })
                .addPathPatterns("/**") // 拦截所有路径
                .excludePathPatterns(
                        // 登录相关接口
                        "/Login",
                        "/Logout",
                        "/reqMSM",

                        // 静态资源
                        "/aaaa/**",
                        "/static/**",
                        "/css/**",
                        "/js/**",
                        "/images/**",
                        "/favicon.ico",

                        // Swagger 相关
                        "/swagger-ui.html",
                        "/swagger-ui/**",
                        "/swagger-resources/**",
                        "/v2/api-docs",
                        "/v2/api-docs-ext",
                        "/doc.html",
                        "/doc",
                        "/webjars/**",
                        "/swagger/**",
                        "/configuration/**",
                        "/csrf",

                        // 其他不需要鉴权的路径
                        "/error",
                        "/test/**",  // 测试接口，可根据实际需要调整
                        "/auth/public/**",  // 公开的测试接口
                        "/auth/checkLogin",  // 检查登录状态接口
                        "/home",  // 跳转主页
                        "/index"  // 跳转主页
                );
    }
}
